﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Tomasulo_Simulator
{
    class InstructionBuffer
    {
        int pc, size, head, tail, occupied;
        Instruction[] instructions;
        public InstructionBuffer(int size, int pc)
        {
            this.pc = pc;
            this.size = size;
            this.head = 0;
            this.tail = 0;
            this.occupied = 0;
            this.instructions = new Instruction[size];
            
        }
        public void Update()
        {
            Instruction headInstruction = instructions[head];
            Instruction tailInstruction = instructions[tail];
            if (tailInstruction == null)
            {
                ///////////////////////
                tailInstruction = new Instruction(5, pc);
                occupied++;
            }
            else
            {
                tailInstruction.Update();
                if (tailInstruction.Ready)
                {
                    tailInstruction.Initialize("ADDI R1,R0,4");//////////////
                    tail++;
                    tail %= size;
                }
            }
            if (occupied > 0 && headInstruction.Ready)
            {
                bool issued = headInstruction.Issue();
                if (issued)
                {
                    head++;
                    head %= size;
                    occupied--;
                }
            }
        }
    }
}
